home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / Getting Started w⁄GX ƒ / Getting Started GX - printing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  10.3 KB  |  363 lines  |  [TEXT/KAHL]

  1. /**
  2.  --
  3.  --        App:        Getting Started w/QD GX (WWDC)
  4.  -- 
  5.  -- 
  6.  --        Version:    1.0     4/93:    added all of the calls required to support the "Getting 
  7.  --                                    Started with QuickDraw™ GX" session at the WWDC '93     
  8.  --
  9.  --                            8/93:    updated file to work with the ß2 "GXified" interface files
  10.  --                            3/94:    dmh - general cleanup/debugging.
  11.  -- 
  12.  --                            4/96 bob    Updated #includes to support changed GX Library names.
  13.  --                                        Updated the note regarding the files needed to run.
  14.  --                                        Updated the copyright date.
  15.  --
  16.  --        File:        Getting Started GX - printing.c
  17.  --
  18.  --
  19.  --        Comments:    This code contains all of the functions required to print the QuickDraw GX shapes we created.
  20.  --                    We use the QuickDraw GX printing method which takes a picture and passes it to the QuickDraw GX 
  21.  --                    printing system. The picture will always contains what is in the front window.
  22.  --
  23.  --                    If QuickDraw GX printing systems has not been installed, the printing menu items in the File
  24.  --                    menu will be dis-abled.
  25.  --
  26.  --
  27.  --        Components:    Getting Started GX - main.c
  28.  --                    Getting Started GX - main.h
  29.  --                    Getting Started GX - shapes.c
  30.  --                    Getting Started GX - printing.c
  31.  --                    Getting Started GX - misc.c
  32.  --                    Getting Started QD GX.π.rsrc
  33.  --
  34.  --                    The file titled: "Getting Started GX - main.c" contains the code required to
  35.  --                    intialize and tear down the QuickDraw GX world, and the event loop.
  36.  --
  37.  --                    The file titled: "Getting Started GX - shapes.c" contains of the code used to 
  38.  --                    create and manipulate the shapes draw into the window.
  39.  --        
  40.  --        
  41.  --        QuickDraw GX
  42.  --        Libraries
  43.  --        Used:        This application uses the following QuickDraw GX library code files:
  44.  --                    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c", "LayoutLibrary.c",
  45.  --                    "ShapeLibrary.c", "TransferModeLibrary.c", and "TransformLibrary.c".
  46.  --        
  47.  --        
  48.  --        Notes:        1) Print this file in landscape for the best results
  49.  --                    2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
  50.  --                    3) This code was adapted from the "Banana Jr." QuickDraw GX sample.
  51.  --
  52.  --
  53.  --        Author:        Pete "Luke" Alexander
  54.  --                    Developer Technical Support
  55.  --                    AppleLink: DEVSUPPORT
  56.  --
  57.  --        
  58.  --        ©1992 - 1996  Apple Computer, Inc. 
  59.  --        All rights reserved.
  60.  --
  61.  **/
  62.  
  63. #include <GXPrinting.h>
  64. #include "GXExceptions.h"
  65. #include "memory.h"
  66. #include "Getting Started GX - main.h"
  67.  
  68.  
  69. /*------ SetUpEditMenuRec ------------------------------------------------------------------------------------*/
  70. //
  71. //    This routine sets up an gxEditMenuRecord which references our edit menu.  This structure
  72. //    is used by the GXJobDefaultFormatDialog and GXJobPrintDialog calls to allow cut, copy, & paste
  73. //    operations from the dialogs.
  74. //
  75. void SetUpEditMenuRec(gxEditMenuRecord *edMenuRec)
  76. {
  77.  
  78.     edMenuRec->editMenuID = mEdit;
  79.     edMenuRec->cutItem = iCut;
  80.     edMenuRec->copyItem = iCopy;
  81.     edMenuRec->pasteItem = iPaste;
  82.     edMenuRec->clearItem = iClear;
  83.     edMenuRec->undoItem = iUndo;
  84. }
  85.  
  86.  
  87. /*------ DoFormat ------------------------------------------------------------------------------------*/
  88. //
  89. //    This routine performs GX's equivalent of the Page Setup (PrStlDialog) call of
  90. //    the old printing architecture.
  91. //
  92. OSErr DoFormat(WindowPtr theWindow, gxDialogResult    *result)
  93. {
  94.     OSErr                err;
  95.     gxEditMenuRecord    edMenuRec;
  96.     gxJob                documentJob;
  97.  
  98.     //
  99.     //    If we have a non-nil WindowPtr, set up our edit menu record and handle the job
  100.     //    format dialog.  Remember to check for errors.
  101.     //
  102.     if (theWindow)
  103.     {
  104.         documentJob = GetDocJob(theWindow);
  105.         SetUpEditMenuRec(&edMenuRec);
  106.         *result = GXJobDefaultFormatDialog(documentJob, &edMenuRec);
  107.     }
  108.     
  109.     return GXGetJobError(documentJob);
  110. }
  111.  
  112.  
  113. /*------ DoPrinting ----------------------------------------------------------------------------------*/
  114. //
  115. //    This routine performs QuickDraw GX's equivalent of the Print gxJob Dialog (PrJobDialog) call of
  116. //    the old printing architecture, and then prints the document if the user wants to.
  117. //
  118. OSErr DoPrinting(WindowPtr theWindow)
  119. {
  120.     OSErr                err = noErr;
  121.     gxDialogResult        result;
  122.     gxEditMenuRecord    edMenuRec;
  123.     gxJob                docJob;
  124.  
  125.  
  126.     //
  127.     //    If we have a non-nil WindowPtr, set up our edit menu record and handle the print
  128.     //    job dialog.  Remember to check for errors.  If no errors occur, and the user clicks
  129.     //  ok, print the window's document.
  130.     //
  131.     if (theWindow)
  132.     {
  133.         docJob = GetDocJob(theWindow);
  134.         
  135.         SetUpEditMenuRec(&edMenuRec);
  136.         result = GXJobPrintDialog(docJob, &edMenuRec);
  137.         err = GXGetJobError(docJob);
  138.                 
  139.         if ((result == gxOKSelected) && !(err))
  140.             err = DoPrintLoop(theWindow);
  141.     }
  142.     
  143.     return err;
  144. }
  145.  
  146.  
  147.  
  148. /*------ DoPrintLoop ----------------------------------------------------------------------------------*/
  149. //
  150. //    This routine prints one copy of the window's document using whatever job and format
  151. //    is currently attached to it.  (If the window wwere just created and then the user
  152. //    selected Print One Copy w/o first selecting Page Setup…, the system default job and
  153. //    format are stored with this document.
  154. //
  155. OSErr DoPrintLoop(WindowPtr theWindow)
  156. {
  157.     OSErr        err = noErr;
  158.     gxJob        documentJob;
  159.     Str255        windowTitle;
  160.  
  161.     //
  162.     //    If we have a non-nil WindowPtr, start the print job, print a page and then finish
  163.     //    the job.  Since we have just a one page document, we don't bother counting pages.
  164.     //    Remember to check those errors!
  165.     //
  166.     if (theWindow)
  167.     {
  168.         documentJob = GetDocJob(theWindow);
  169.         GetWTitle(theWindow, windowTitle);
  170.     
  171.         GXStartJob(documentJob, windowTitle, 1);
  172.         err = GXGetJobError(documentJob);
  173.         
  174.         if (!err)
  175.         {
  176.               GXPrintPage(documentJob, 1,GXGetJobFormat(documentJob, 1), GetDocShape(theWindow));
  177.             err = GXGetJobError(documentJob);
  178.         }
  179.         
  180.         GXFinishJob(documentJob);
  181.         if (!err) err = GXGetJobError(documentJob);
  182.     }
  183.     return err;
  184. }
  185.  
  186.  
  187.  
  188. /*******************************************************************
  189.     DoPrintOneCopy sets up our job collection items for printing
  190.     one copy of a document, and then prints the document.
  191.     
  192. ********************************************************************/
  193.  
  194. OSErr DoPrintOneCopy(WindowPtr wind)
  195. {
  196.     OSErr                    err;
  197.     Collection                jobCollection;
  198.     gxCopiesInfo            copiesInfo;
  199.     gxFileDestinationInfo    destInfo;
  200.     gxPageRangeInfo            pageRangeInfo;
  201.     Ptr                        oldCopiesInfo = nil, oldPageRangeInfo = nil, oldDestInfo = nil;
  202.     long                    oldCopiesSize, oldPageRangeInfoSize, oldDestInfoSize;
  203.     TH_Doc                    doc = (TH_Doc) GetWRefCon(wind);
  204.  
  205. /* Get the job collection and set it up to print one copy…    */
  206.  
  207.     jobCollection = GXGetJobCollection(GetDocJob(wind));
  208.  
  209.  
  210. /* Set number of copies to 1.            */
  211.     
  212.     copiesInfo.copies = 1;
  213.     err = MyReplaceCollectionItem(&copiesInfo, sizeof(gxCopiesInfo),
  214.                                     gxCopiesTag, gxPrintingTagID,
  215.                                     jobCollection, &oldCopiesInfo, &oldCopiesSize);
  216.     nrequire(err, ReplaceCopies_error);
  217.  
  218.  
  219. /* Set page range to "all".    */
  220.     
  221.     pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
  222.     pageRangeInfo.minFromPage = 1;
  223.     pageRangeInfo.simpleRange.fromPage = 1;        // This app only handles one page docs.
  224.     pageRangeInfo.maxToPage = 1;
  225.     pageRangeInfo.simpleRange.toPage = 1;
  226.     pageRangeInfo.simpleRange.printAll = true;
  227.     err = MyReplaceCollectionItem(&pageRangeInfo, sizeof(gxPageRangeInfo),
  228.                                     gxPageRangeTag, gxPrintingTagID,
  229.                                     jobCollection, &oldPageRangeInfo, &oldPageRangeInfoSize);
  230.     nrequire(err, ReplacePageRange_error);
  231.  
  232.  
  233. /* Set destination to "printer".        */
  234.  
  235.     destInfo.toFile = false;
  236.     err = MyReplaceCollectionItem(&destInfo, sizeof(gxFileDestinationInfo),
  237.                                   gxFileDestinationTag, gxPrintingTagID,
  238.                                   jobCollection, &oldDestInfo, &oldDestInfoSize);
  239.     nrequire(err, ReplaceDestination_error);
  240.  
  241.  
  242. /* Print one copy of our document.        */
  243.  
  244.     err = DoPrintLoop(wind);
  245.  
  246.  
  247. /*    Restore original number of copies, page range, and output
  248.     destination in case anybody uses that info. */
  249.  
  250. ReplaceCopies_error:
  251.     MyReplaceCollectionItem(oldCopiesInfo, oldCopiesSize,
  252.                               gxCopiesTag, gxPrintingTagID,
  253.                             jobCollection, nil, nil);
  254.  
  255. ReplacePageRange_error:
  256.     MyReplaceCollectionItem(oldPageRangeInfo, oldPageRangeInfoSize,
  257.                             gxPageRangeTag, gxPrintingTagID,
  258.                             jobCollection, nil, nil);
  259.  
  260. ReplaceDestination_error:
  261.     MyReplaceCollectionItem(oldDestInfo, oldDestInfoSize,
  262.                             gxFileDestinationTag, gxPrintingTagID,
  263.                             jobCollection, nil, nil);
  264.  
  265.  
  266. /* Dispose of the pointers that MyReplaceCollectionItem created. */
  267.  
  268.     if (oldCopiesInfo)
  269.         DisposePtr(oldCopiesInfo);
  270.  
  271.     if (oldPageRangeInfo)
  272.         DisposePtr(oldPageRangeInfo);
  273.  
  274.     if (oldDestInfo)
  275.         DisposePtr(oldDestInfo);
  276.  
  277.     return err;
  278. }
  279.  
  280.  
  281.  
  282. /*******************************************************************
  283.     MyReplaceCollectionItem is a generic routine that replaces a
  284.     collection item with the passed data.  If the oldData parameter
  285.     is not nil, this routine creates a pointer and returns the data
  286.     that's being replaced in it.  (If the item doesn't already exist,
  287.     then a copy of the newData is returned instead.)
  288.     
  289. ********************************************************************/
  290.  
  291. OSErr MyReplaceCollectionItem(void *newData, long collectSize,
  292.                               OSType collectType, long collectID,
  293.                               Collection whichCollection,
  294.                               Ptr *oldData, long *oldDataSize)
  295. {
  296.     OSErr    err = noErr;
  297.     long    index;
  298.  
  299. /*
  300.     If we're supposed to return the old data, get it.
  301.     If there is no old data, return a copy of the new data.
  302. */
  303.  
  304.     if (oldData)
  305.     {
  306.         err = GetCollectionItemInfo(whichCollection,
  307.                                     collectType,
  308.                                     collectID,
  309.                                     dontWantIndex,
  310.                                     oldDataSize,
  311.                                     dontWantAttributes);
  312.  
  313.         if (err)
  314.         {
  315.             *oldDataSize = collectSize;
  316.             *oldData = NewPtrSys(*oldDataSize);
  317.             if (!(err = MemError()))
  318.                 BlockMove(newData, *oldData, collectSize);
  319.         }
  320.         else
  321.         {
  322.             *oldData = NewPtrSys(*oldDataSize);
  323.             if (!(err = MemError()))
  324.                 err = GetCollectionItem(whichCollection,
  325.                                         collectType,
  326.                                         collectID,
  327.                                         dontWantSize,
  328.                                         *oldData);
  329.         }
  330.     }
  331.  
  332.     nrequire(err, CouldNotSetOldData);
  333.  
  334.  
  335. // If we're adding a new collection item, do so.  Otherwise, get the
  336. // existing item's index and replace the old collection item.
  337.  
  338.     err = AddCollectionItem(whichCollection,
  339.                             collectType,
  340.                             collectID,
  341.                             collectSize,
  342.                             newData);
  343.  
  344.     if (err == collectionItemLockedErr)
  345.     {
  346.         err = GetCollectionItemInfo(whichCollection,
  347.                                     collectType,
  348.                                     collectID,
  349.                                     &index,
  350.                                     dontWantSize,
  351.                                     dontWantAttributes);
  352.         if (!err)
  353.             err = ReplaceIndexedCollectionItem(whichCollection,
  354.                                                index,
  355.                                                collectSize,
  356.                                                newData);
  357.     }
  358.  
  359. CouldNotSetOldData:
  360.     return err;
  361. }
  362.  
  363.